import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.*;
//Current Status - The manufacturer method is working. 
//TO DO - Make the other methods return an arraylist for the drop down menu to keep updating.
//TO DO - Make the vendor to be working to.
// TO DO - Insertion,deletion, Update - Add a text area at LAyout.SOUTH

public class QueryGui extends JFrame 
{
	ArrayList<String> values = new ArrayList<String>();// The selected values.


	private JTextField displayField = new JTextField(30);

	private JComboBox c = new JComboBox();

	private JButton b = new JButton("Add item");

	private int count = 0;

	public static void main(String[] args) 
	{
		new QueryGui();
	}	 

	public QueryGui() 
	{
		ConnectDb connectDb = new ConnectDb();
		Connection connection = connectDb.getDBConnection();

		String [] menu = {"Enter M for Search by Manufacturer.", "Enter V for searching by vendor and part number","Enter Q to quit."};

		String option = JOptionPane.showInputDialog(menu); // About to work on this.- take the input from the user to know what he wants.

		//-------------------------------------------------------------------
		final Query q = new Query(connection, connectDb);
		//queryVendorPart q2 = new queryVendorPart(connection, connectDb);;
		
	
		 final ArrayList<String> temp = q.queryManufac();
		 final int x = temp.size();
	//	if(option.equals("M"))
		
			
		displayField.setEditable(false);
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {	
				c.removeAllItems();
			
				for(int i = 0;i<x;i++)
					c.addItem(temp.get(i));
				//System.out.println(e.getSource().toString());
			}
		});
		connectDb.disconnectFromDB();

		c.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//if()
				//{
					count++;
					displayField.setText("index: " + c.getSelectedIndex() + "   "+ ((JComboBox) e.getSource()).getSelectedItem());
			//	}
				//else
				//	displayField.setText("index: " + c.getSelectedIndex() + "   "+ ((JComboBox) e.getSource()).getSelectedItem());

			}
		});

		JPanel displayPanel = new JPanel() ; // the upper panel.
		JPanel optionPanel = new JPanel() ; // the 2nd panel which displays the content.

		optionPanel.setLayout(new FlowLayout());
		displayPanel.add(displayField);
		optionPanel.add(c);
		optionPanel.add(b);

		JFrame frame = new JFrame();
		frame.setLayout(new BorderLayout());
		frame.add(displayPanel,BorderLayout.NORTH);
		frame.add(optionPanel,BorderLayout.CENTER);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 400);
		frame.setVisible(true);

	}

	public void addManuf(ArrayList<String> al)
	{
		if(count == 0)
		{
			for(int i = 0;i<al.size();i++)
				c.addItem(al.get(i));
		}
		else
			c.addItem("Insert");

	}
}

